package com.tarena.lbs.business.servcie.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.StoreChainRepository;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreChainParam;
import com.tarena.lbs.business.pojo.po.StoreChainPO;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreChainQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.StoreChainVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StoreChainServiceImpl implements StoreChainService {
    @Autowired
     private StoreChainRepository storeChainRepository;
    @Autowired
     private StoreRepository storeRepository;
    @Autowired
     private BusinessServiceImpl businessService;
    @Override
    public PageResult<StoreChainVO> pageList(StoreChainQuery query) {
        PageResult<StoreChainVO> voPage = new PageResult<>();
        PageInfo<StoreChainPO> pageInfo = storeChainRepository.pageList(query);
//        log.debug("分页查询结果: {}", pageInfo);
        // 将分页信息中的页码设置到分页结果对象中
        voPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        voPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        voPage.setTotal(pageInfo.getTotal());

        List<StoreChainVO> vos = null;
        List<StoreChainPO> pos = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(pos)) {
            vos = pos.stream().map(po -> {
                StoreChainVO vo = new StoreChainVO();
                BeanUtils.copyProperties(po, vo);
                //将门店信息封裝到查出的結果中
                StorePO spo = storeRepository.storeEdit(vo.getStoreId());
                StoreVO svo = new StoreVO();
                BeanUtils.copyProperties(spo, svo);
//                log.debug("查出的门店信息: {}", svo);
                vo.setStore(svo);
                //將商家信息封裝到查出 的結果中
                BusinessStoreVO business = businessService.businessStoreDetail(spo.getBusinessId());
//                log.debug("查出的商家信息: {}", business);
                vo.setBusiness(business);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);

        return voPage;
    }

    @Override
    public void update(StoreChainParam param) {
        StoreChainPO po = assembleBusinessPO(param);
        log.debug("修改二维码,参数:{}", po);
        storeChainRepository.update(po);
    }

    @Override
    public void saveStoreChain(StoreChainParam param) {
        StoreChainPO po = assembleBusinessPO(param);
        storeChainRepository.saveStoreChain(po);
    }

    /**
     * 页面参数转换数据库实体类
     *
     * @param param
     * @return
     */
    private StoreChainPO assembleBusinessPO(StoreChainParam param) {
        log.debug("参数:{}", param);
        StoreChainPO po = new StoreChainPO();
        BeanUtils.copyProperties(param, po);
        log.debug("assembleBusinessPO参数:{}", po);
        if (param.getId() == null) {
            //创建时间
            po.setCreateTime(new Date());
        }
        return po;
    }
}